package com.cw.jeeyt.service.impl.git;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.service.bo.git.GitSendEmailRecordBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.git.GitSendEmailRecordDao;
import com.cw.jeeyt.dao.model.git.GitSendEmailRecordDo;
import com.cw.jeeyt.service.intf.git.GitSendEmailRecordService;
import com.cw.jeeyt.service.converter.git.GitSendEmailRecordConverter;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 邮件发送通知记录业务实现类
 *
 * @author chenw
 * @date 2019-02-25 01:04:34
 */
@Service
public class GitSendEmailRecordServiceImpl implements GitSendEmailRecordService {

    private static final Logger logger = LoggerFactory.getLogger(GitSendEmailRecordServiceImpl.class);
    @Autowired
    private GitSendEmailRecordDao gitSendEmailRecordDao;//邮件发送通知记录数据库操作接口

    /**
     * 查询所有邮件发送通知记录
     */
    @Override
    public List<GitSendEmailRecordBo> queryGitSendEmailRecordList() throws Exception {
        return GitSendEmailRecordConverter.doList2BoList(gitSendEmailRecordDao.queryGitSendEmailRecordList());
    }

    /**
     * 分页查询邮件发送通知记录
     *
     * @param gitSendEmailRecordBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<GitSendEmailRecordBo> queryByPage(GitSendEmailRecordBo gitSendEmailRecordBo, Integer startNum,
        Integer pageSize, Order order) throws Exception {

        if (gitSendEmailRecordBo == null) {
            logger.error("邮件发送通知记录为空");
            return null;
        }
        PageInfo<GitSendEmailRecordDo> pageInfo = gitSendEmailRecordDao.queryByPage(
            GitSendEmailRecordConverter.bo2Do(gitSendEmailRecordBo), startNum, pageSize, order);
        return new PageInfo<>(pageInfo, GitSendEmailRecordConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查询邮件发送通知记录信息
     *
     * @return
     */
    @Override
    public GitSendEmailRecordBo getGitSendEmailRecordById(Long recordId) throws Exception {

        if (null == recordId) {
            logger.error("--主键recordId为空--");
            return null;
        }
        return GitSendEmailRecordConverter.do2Bo(gitSendEmailRecordDao.getById(recordId));
    }

    /**
     * 新增邮件发送通知记录
     *
     * @param gitSendEmailRecordBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(GitSendEmailRecordBo gitSendEmailRecordBo) throws Exception {

        if (gitSendEmailRecordBo == null) {
            logger.error("邮件发送通知记录为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        GitSendEmailRecordDo gitSendEmailRecord = GitSendEmailRecordConverter.bo2Do(gitSendEmailRecordBo);
        gitSendEmailRecord.setCreateBy(userBo.getId());
        gitSendEmailRecord.setCreateDate(new Date());
        gitSendEmailRecord.setUpdateBy(userBo.getId());
        gitSendEmailRecord.setUpdateDate(new Date());
        Integer result = gitSendEmailRecordDao.insert(gitSendEmailRecord);
        return result;
    }

    /**
     * 新增邮件发送通知记录不为空的字段
     *
     * @param gitSendEmailRecordBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertSelective(GitSendEmailRecordBo gitSendEmailRecordBo) throws Exception {

        if (gitSendEmailRecordBo == null) {
            logger.error("邮件发送通知记录为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        GitSendEmailRecordDo gitSendEmailRecord = GitSendEmailRecordConverter.bo2Do(gitSendEmailRecordBo);
        gitSendEmailRecord.setCreateBy(userBo.getId());
        gitSendEmailRecord.setCreateDate(new Date());
        gitSendEmailRecord.setUpdateBy(userBo.getId());
        gitSendEmailRecord.setUpdateDate(new Date());
        Integer result = gitSendEmailRecordDao.insertSelective(gitSendEmailRecord);
        return result;
    }

    /**
     * 更新邮件发送通知记录
     *
     * @param gitSendEmailRecordBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(GitSendEmailRecordBo gitSendEmailRecordBo) throws Exception {

        if (gitSendEmailRecordBo == null) {
            logger.error("邮件发送通知记录为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        GitSendEmailRecordDo gitSendEmailRecord = GitSendEmailRecordConverter.bo2Do(gitSendEmailRecordBo);
        gitSendEmailRecord.setUpdateBy(userBo.getId());
        gitSendEmailRecord.setUpdateDate(new Date());

        gitSendEmailRecordDao.update(gitSendEmailRecord);
    }

    /**
     * 更新邮件发送通知记录不为空的字段
     *
     * @param gitSendEmailRecordBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(GitSendEmailRecordBo gitSendEmailRecordBo) throws Exception {

        if (gitSendEmailRecordBo == null) {
            logger.error("邮件发送通知记录为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        GitSendEmailRecordDo gitSendEmailRecord = GitSendEmailRecordConverter.bo2Do(gitSendEmailRecordBo);
        gitSendEmailRecord.setUpdateBy(userBo.getId());
        gitSendEmailRecord.setUpdateDate(new Date());

        gitSendEmailRecordDao.updateSelective(gitSendEmailRecord);
    }

    /**
     * 删除邮件发送通知记录
     *
     * @param gitSendEmailRecordBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(GitSendEmailRecordBo gitSendEmailRecordBo) throws Exception {

        if (gitSendEmailRecordBo == null) {
            logger.error("邮件发送通知记录为空");
            return;
        }
        GitSendEmailRecordDo gitSendEmailRecord = GitSendEmailRecordConverter.bo2Do(gitSendEmailRecordBo);
        gitSendEmailRecordDao.delete(gitSendEmailRecord);
    }
}
